//
//  YHEmoticonPageViewCell.swift
//  YHWeibo
//
//  Created by yuhao.mac on 16/7/5.
//  Copyright © 2016年 yuhao.mac. All rights reserved.
//

import UIKit

class YHEmoticonPageViewCell: UICollectionViewCell {
    
    var emoticons:[YHEmotcion]?{
        didSet{
            
            //每当外界给其设置值的时候调用，由于复用问题，需要先将所有按钮隐藏
            for button in emoticonButtons{
                button.hidden = true
            }
            
            //遍历外界传入的一维数组
            for (i , emoticon) in emoticons!.enumerate() {
                //获取button
                let button = emoticonButtons[i]
                button.hidden = false
                
                //是emoji表情
                if emoticon.isEmoji{
                    
                    let title = ((emoticon.code ?? "") as NSString).emoji()
                    
                    button.setTitle(title, forState: UIControlState.Normal)
                    button.setImage(nil, forState: UIControlState.Normal)
                
                }else{
                    let path = emoticon.path ?? ""
                    let png = emoticon.png ?? ""
                    let name = path + png
                    let image = UIImage(named: name, inBundle: YHEmotcionTools.sharedTools.emotcionBundle, compatibleWithTraitCollection: nil)
                    
                    button.setImage(image, forState: UIControlState.Normal)
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
                
            }
            
        }
    }
    
    //保存20个表情按钮的数组
    var emoticonButtons:[UIButton] = [UIButton]()
    
    var indexPath: NSIndexPath?{
        didSet{
            messageLabel.text = "第\(indexPath?.section ?? 0)组 第\(indexPath?.item ?? 0)页"
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = RandomColor()
        addChildButtons()
        
        contentView.addSubview(messageLabel)
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(contentView)
        }
    }
    
    //设置按钮的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置按钮的宽高
        let w = SCREENW / CGFloat(EmoticonMaxCol)
        let h = (216 - 37) / CGFloat(EmoticonMaxRow)
        
        //遍历存放数组的按钮   ????
        for (i , button) in emoticonButtons.enumerate(){
            let x = i % EmoticonMaxCol
            let y = i / EmoticonMaxCol
            button.frame = CGRect(x: CGFloat(x) * w, y: CGFloat(y) * h, width: w, height: h)
        }
    }
    
    //创建20个按钮
    func addChildButtons(){
        for _ in 0..<EmoticonMaxCount{
            let button = UIButton()
            button.titleLabel?.font = UIFont.systemFontOfSize(32) 
            button.backgroundColor = RandomColor()
            //添加到一个数组中
            emoticonButtons.append(button)
            
            contentView.addSubview(button)
        }
    }
    
    private lazy var messageLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.redColor(), textFont: 32)
        lab.textAlignment = .Center
        return lab
    }()
}
